package com.aat.dxfy.smscheck;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;  
  
/** 
 * User: fafu 
 * Date: 14-7-25 
 * Time: 下午5:37 
 * This class is 
 */  
public class TrieCheck implements Check {  
    private TrieNode root;  
  
    @Override  
    public void addWord(String word) {  
        if (word == null || word.length() == 0) return;  
        TrieNode current = root;  
        for (int i = 0; i < word.length(); i++) {  
            char code = word.charAt(i);  
            current = current.add(code);  
        }  
        current.end = true;  
    }  
  
    @Override  
    public boolean hasBadWord(String text) {  
        IndexWordPair pair = getBaddWord(text);  
        if (pair == null) return false;  
        return true;  
    }  
  
    private IndexWordPair getBaddWord(String text) {  
        if (text == null || text.length() == 0) return null;  
        List<Character> chlist = new ArrayList<Character>();  
        for (int i = 0; i < text.length(); i++) {  
            TrieNode current = root;  
            int index = i;  
            while ((current = current.child.get(text.charAt(index))) != null) {  
                chlist.add(text.charAt(index));  
                if (current.end) {  
                    return new IndexWordPair(index, Arrays.toString(chlist.toArray(new Character[0])));  
                }  
                if (text.length() == ++index) {  
                    break;  
                }  
            }  
        }  
        return null;  
    }  
  
    @Override  
    public String replaceWith(String text, char mark) {  
        if (text == null || text.length() == 0) return null;  
        char[] ca = text.toCharArray();  
        for (int i = 0; i < text.length(); i++) {  
            List<Character> chlist = new ArrayList<Character>();  
            TrieNode current = root;  
            while ((current = current.child.get(ca[i])) != null) {  
                chlist.add(ca[i]);  
                if (current.end) {  
                    for (int idx = 0; idx < chlist.size(); idx++) {  
                        ca[idx] = mark;  
                    }  
                    break;  
                }  
                if (text.length() == i+1) {  
                    break;  
                }  
            }  
        }  
        StringBuilder sb = new StringBuilder();  
        for(char c:ca){  
            sb.append(c);  
        }  
        return sb.toString();  
    }  
  
    private static class TrieNode {  
        public Map<Character, TrieNode> child = new HashMap<Character, TrieNode>();  
        private boolean end = true;  
  
        public TrieNode() {  
  
        }  
  
        public TrieNode add(char newChar) {  
            TrieNode t = child.get(newChar);  
            if (t == null) t = new TrieNode();  
            child.put(newChar, t);  
            return t;  
        }  
    }  
  
    private class IndexWordPair {  
  
        public IndexWordPair(int index, String word) {  
        }  
    }  
}  